package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.MenuForMain;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping("/findAll")
    //查询所有
    public Result findAll() {
        //调用service返回checkItem list集合
        List<Menu> menuList = menuService.findAll();
        if (menuList != null && menuList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS);
            result.setData(menuList);
            return result;
        } else {
            return new Result(false, MessageConstant.QUERY_EMNU_FAIL);
        }
    }

    //设置修改权限
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping("/findPage")
    //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用分页查询方法
        PageResult pageResult = menuService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        //返回PageResult
        return pageResult;
    }

    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping("/findAllParentMenu")
    //查询所有
    public Result findAllParentMenu() {
        //调用service返回checkItem list集合
        List<Menu> menuList = menuService.findAllParentMenu();
        if (menuList != null && menuList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS);
            result.setData(menuList);
            return result;
        } else {
            return new Result(false, MessageConstant.QUERY_EMNU_FAIL);
        }
    }

    //设置权限
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        //try
        try {
            //调用add方法
            menuService.add(menu);
            //返回添加成功
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回添加失败
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    //设置修改权限
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping("/findById")
    //通过id查询
    public Result findById(Integer id) {
        //调用id查询方法
        Menu menu = menuService.findById(id);
        //判断数据是否为空
        if (menu != null) {
            //数据不为空新建查询成功Result
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS);
            //result调用setData方法吧查询结果添加进result
            result.setData(menu);
            //返回result
            return result;
        } else {
            //返回查询失败
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //添加编辑权限
    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody Menu menu) {
        //try
        try {
            //调用编辑方法
            menuService.edit(menu);
            //返回编辑成功
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回编辑失败
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id) {
        //try
        try {
            //调用删除方法
            menuService.delete(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            //catch自定义错误并返回
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        } catch (Exception e) {
            //返回删除失败
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        //返回删除成功
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping("/getAllMenusForCurrentUser")
    //查询所有
    public Result getAllMenusForCurrentUser(String username) {
        //调用service返回checkItem list集合
        List<MenuForMain> menuList = menuService.getAllMenusForCurrentUserFromRedis(username);
        if (menuList != null && menuList.size() > 0){
            System.out.println("");
            menuList = menuService.getAllMenusForCurrentUser(username);
        }
        if (menuList != null && menuList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_MENU_SUCCESS);
            result.setData(menuList);
            return result;
        } else {
            return new Result(false, MessageConstant.QUERY_EMNU_FAIL);
        }
    }
}
